<?php defined('SYSPATH') or die('No direct script access.');

/**
 * Cms controller class
 *
 * @package    kohana
 * @category   Controller
 * @author     Salomon
 * @copyright (c) 2012 Salomon
 * @license    http://creativecommons.org/licenses/BSD/
 */
class Controller_Module_Content extends Controller_Manager_Template_Default {

    private $view;
    private $view_id = 'content/';
    private $model;
    private $model_id = 'content';    
    private $table_name = 'contents';
    private $result;
    
    public $section_name = 'Conteúdo';
    public $section_title = 'Conteúdos';
    
    public function before()
    {
        parent::before();

        if (!Auth::instance()->logged_in())
        {
            $this->request->redirect('manager/login');
        }

        $this->model = ORM::factory($this->model_id);
        
    }

    /**
        * Default action method for the controller
        *
        * @return  void
        */
    public function action_index()
    {

        $this->model = ORM::factory($this->model_id);
        $this->result = $this->model->find_all();

        $list = array();
        foreach($this->result as $user) {
             $obj = new stdClass();
             $obj->name = $user->name;
             $obj->title = $user->title;
             $obj->text = $user->text;
             $obj->image = $user->image;
             $obj->publish = $user->publish;
             $obj->highlight = $user->highlight;
             $obj->id = $user->id;
             
             $list[] = $obj;
        }
                
        $this->view = View::factory($this->view_id.'list');
        $this->view->set('list', $list);        
        $this->view->set('section_name', $this->section_name);        
        $this->view->set('section_title', $this->section_title);        
        
        $this->template->content = $this->view;        

    }
    
    function action_create()
    {
        
        $id = $this->request->param('id');
        if(!empty($id))
        {            
            $this->model = ORM::factory($this->model_id, $id);
        }
         
        $this->view = View::factory($this->view_id.'create');
        $this->view->set('model', $this->model);        
        $this->view->set($this->model_id.'_id', $id);
        $this->template->content = $this->view;
        
    }
    
    function action_delete()
    {
        $this->model = ORM::factory($this->model_id,$this->request->post('id'));
        $this->model->delete();
        
        exit;
    }
    
    function action_deleteselected()
    {
        
        $ids = $this->request->post('id');
                
        $this->result = DB::delete($this->table_name)->where('id', 'IN', $ids)->execute();
        
        if($this->result > 0)
        {
            echo "1";
        }
        else
        {
            echo "0";
        }
            
        exit;
    }    
    
    function action_save() 
    {
        
    }


} 